package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.foxcard.FoxSaleCard;
import com.yanqu.road.entity.activity.foxcard.UserFoxCardData;
import com.yanqu.road.entity.activity.foxcard.UserFoxCardShop;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserFoxCardShopDaoImpl extends TempDataObjectDao<UserFoxCardShop> {

    public static String getUserFoxCardShopSql = "select * from `t_u_activity_fox_shop` where `activityId`=? and `userId`=?";
    public UserFoxCardShop getUserFoxCardShop(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserFoxCardShopSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserFoxCardShopSql = "update `t_u_activity_fox_shop` set saleCards=?,refreshAmount=?,refreshPrice=?,freeTimes=?,inPoolCardIds=?,outPoolCardIds=?,coupon=?,cardPacks=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserFoxCardShop userFoxCardShop) {
        userFoxCardShop.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getSaleCards())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardShop.getRefreshAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardShop.getRefreshPrice()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardShop.getFreeTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getInPoolCardIds())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getOutPoolCardIds())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getCoupon())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getCardPacks())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardShop.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardShop.getUserId()));
        if (getDb().executeNoneQuery(updateUserFoxCardShopSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserFoxCardShopSql = "insert into `t_u_activity_fox_shop`(activityId,userId,saleCards,refreshAmount,refreshPrice,freeTimes,inPoolCardIds,outPoolCardIds,coupon,cardPacks) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserFoxCardShop userFoxCardShop) {
        userFoxCardShop.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardShop.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardShop.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getSaleCards())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardShop.getRefreshAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardShop.getRefreshPrice()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardShop.getFreeTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getInPoolCardIds())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getOutPoolCardIds())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getCoupon())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardShop.getCardPacks())));
        if (getDb().executeNoneQuery(insertUserFoxCardShopSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserFoxCardShop parseData(ResultSet rs) throws SQLException {
        UserFoxCardShop userFoxCardShop = new UserFoxCardShop();
        userFoxCardShop.setActivityId(rs.getInt("activityId"));
        userFoxCardShop.setUserId(rs.getLong("userId"));
        userFoxCardShop.setSaleCards(JSON.parseObject(rs.getString("saleCards"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.foxcard.FoxSaleCard>>() {}));
        userFoxCardShop.setRefreshAmount(rs.getInt("refreshAmount"));
        userFoxCardShop.setRefreshPrice(rs.getInt("refreshPrice"));
        userFoxCardShop.setFreeTimes(rs.getInt("freeTimes"));
        userFoxCardShop.setInPoolCardIds(JSON.parseObject(rs.getString("inPoolCardIds"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userFoxCardShop.setOutPoolCardIds(JSON.parseObject(rs.getString("outPoolCardIds"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userFoxCardShop.setCoupon(JSON.parseObject(rs.getString("coupon"), FoxSaleCard.class));
        userFoxCardShop.setCardPacks(JSON.parseObject(rs.getString("cardPacks"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.foxcard.FoxCardPack>>() {}));

        return userFoxCardShop;
    }
}
